﻿
function CheckComma() {
    var sms = document.getElementById('txt_Phones');
    var oldValue = sms.value;
    while (oldValue.indexOf("，") != -1)//寻找每一个中文逗号，并替换
    {
        sms.value = oldValue.replace("，", ",");
        oldValue = sms.value;
    }
}

function CheckNum() {
    var smsstr = document.getElementById('txt_Phones').value;
    //验证是否为空

    if (smsstr == "") {
        alert("手机号码不能为空。");
        return false;
    }
    //过滤最后一个为逗号的字符
    var str = smsstr.substring(smsstr.length - 1, smsstr.length);
    if (str == ",") {
        smsstr = smsstr.substring(0, smsstr.length - 1);
    }
    var m_array = smsstr.split(",");
    for (i = 0; i < m_array.length; i++) {
        //检查是否连续输入两个逗号
        if (m_array[i].length == 0) {
            alert("连续输入了两个逗号或者在最开头输入了逗号，请检查。");
            var s = document.getElementById("txt_Phones").createTextRange();
            if (s.findText(",,")) {
                s.select();
                s.collapse(false);
            }
            return false;
        }
        //验证手机号码是否正确
        if (!CheckMobile(m_array[i])) {
            alert(m_array[i] + "的手机号码不正确，请检查。");
            var s = document.getElementById("txt_Phones").createTextRange();
            if (s.findText(m_array[i])) {
                s.select();
                s.collapse(false);
            }
            return false;
        }
    }
    //验证手机号码是否有重复
    var flag = 1;
    for (i = 0; i < m_array.length - 1; i++) {
        for (j = i + 1; j < m_array.length; j++) {
            if (m_array[i] == m_array[j]) {
                alert(m_array[i] + "，这个号码重复，请检查。");
                var s = document.getElementById("txt_Phones").createTextRange();
                if (s.findText(m_array[i])) {
                    s.select();
                    s.collapse(false);
                }
                flag = 0;
                return false;
            }
        }
        if (flag == 0)
            return false;
    }

    return true;
}

function CheckMobile(m) {
    if (m.length > 11) {
        return false;
    }
    else {
        var t = /^13[0-9]{1}[0-9]{8}|^15[0-3]{1}[0-9]{8}|^15[5-9]{1}[0-9]{8}|^18[0]{1}[0-9]{8}|^18[5-9]{1}[0-9]{8}$/;
        return t.test(m);
    }
}
       